plotly basics

Author

Beth Jump

Published

July 14, 2022

Overview

While ggplot2 is wonderful for static charts, plotly is amazing for interactive graphs. When you look at the rendered charts below, play around with them! You can hover over data to see values, zoom in and out and even toggle values on and off.

This is just an overview of how to use plotly. When you want to create future plotly charts, you should use the plotly website for examples. It’s really well done and has lots of code to copy!

Note: when you look for plotly help online, be sure to include “R” in your search (ex: “R plotly how to add title”) as there is also a plotly package for python.

Usage

We’re going to walk through how to make a basic scatterplot.

library(tidyverse)
library(lubridate)
library(plotly)
library(palmerpenguins)

str(penguins)
tibble [344 × 8] (S3: tbl_df/tbl/data.frame)
 $ species          : Factor w/ 3 levels "Adelie","Chinstrap",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ island           : Factor w/ 3 levels "Biscoe","Dream",..: 3 3 3 3 3 3 3 3 3 3 ...
 $ bill_length_mm   : num [1:344] 39.1 39.5 40.3 NA 36.7 39.3 38.9 39.2 34.1 42 ...
 $ bill_depth_mm    : num [1:344] 18.7 17.4 18 NA 19.3 20.6 17.8 19.6 18.1 20.2 ...
 $ flipper_length_mm: int [1:344] 181 186 195 NA 193 190 181 195 193 190 ...
 $ body_mass_g      : int [1:344] 3750 3800 3250 NA 3450 3650 3625 4675 3475 4250 ...
 $ sex              : Factor w/ 2 levels "female","male": 2 1 1 NA 1 2 1 2 NA NA ...
 $ year             : int [1:344] 2007 2007 2007 2007 2007 2007 2007 2007 2007 2007 ...

The way you build a plotly chart is quite similar to how you build a ggplot chart. You start with the plot_ly() function to tell R you’re building a plotly chart. Then you use add_trace() to add layers (lines, bars, etc) to the chart. Within add_trace() you can map variables to x, y, color and other arguments. When you map a variable to an argument, it should be prefixed with a ~. For example: x = ~date, y = ~count.

Here’s code to make a basic scatter plot of bill_length_mm and flipper_length_mm colored by species:

penguins %>%
  plot_ly() %>%
  add_trace(x = ~bill_length_mm, 
            y = ~flipper_length_mm,
            type = "scatter",
            mode = "markers",
            color = ~species)
Warning: Ignoring 2 observations

Note that you can pass the add_trace() information to the plot_ly() function, but if you might add additional layers (traces) to your chart, it’s probably better to just use add_trace(). Here’s how you could make the same chart as above using plot_ly() instead of add_trace():

penguins %>%
  plot_ly(x = ~bill_length_mm, 
          y = ~flipper_length_mm,
          type = "scatter",
          mode = "markers",
          color = ~species)
Warning: Ignoring 2 observations

If you don’t love the default colors, you can change them manually:

penguins %>%
  plot_ly() %>%
  add_trace(x = ~bill_length_mm, 
            y = ~flipper_length_mm,
            type = "scatter",
            mode = "markers",
            color = ~species,
            colors = c("green", "orange", "blue"))
Warning: Ignoring 2 observations

You can also easily add labels to your chart.

penguins %>%
  plot_ly() %>%
  add_trace(x = ~bill_length_mm, 
            y = ~flipper_length_mm,
            type = "scatter",
            mode = "markers",
            color = ~species,
            colors = c("green", "orange", "blue")) %>%
  layout(title = "Flipper and bill length of Palmer Penguins", 
         xaxis = list(title = "Bill Length (mm)"),
         yaxis = list(title = "Flipper Length (mm)"))
Warning: Ignoring 2 observations